package cn.tedu.mall.book.service.impl;

import cn.tedu.mall.book.entity.Category;
import cn.tedu.mall.book.mapper.CategoryMapper;
import cn.tedu.mall.book.pojo.dto.CategoryAddNewDTO;
import cn.tedu.mall.book.pojo.vo.CategoryListVO;
import cn.tedu.mall.book.pojo.vo.CategorySimpleVO;
import cn.tedu.mall.book.pojo.vo.CategoryTree;
import cn.tedu.mall.book.service.ICategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiayiyou
 * @data 2022/6/9
 */

@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public void addNew(CategoryAddNewDTO categoryAddNewDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryAddNewDTO,category);
        if (categoryAddNewDTO.getParentId()==null){
            category.setParentId(0);
            category.setDepth(1);
            category.setIsParent(0);
        }else {
            CategorySimpleVO categorySimple = categoryMapper.getCategorySimpleById(categoryAddNewDTO.getParentId());
            category.setDepth(categorySimple.getDepth()+1);
            category.setIsParent(1);
        }
        category.setGmtCreate(LocalDateTime.now());
        category.setGmtModified(LocalDateTime.now());
        categoryMapper.insert(category);
    }

    /**
     * 查询所有分类
     * @return 包含查询分类的一个list
     */
    @Override
    public List<CategorySimpleVO> getCategory() {
        List<CategorySimpleVO> list = categoryMapper.getCategory();
        return list;
    }

    /**
     * 根据一级分类id查询二级分类
     */
    @Override
    public List<CategorySimpleVO> getChildrenCategoryByParentId(Integer parentId) {
        List<CategorySimpleVO> list=categoryMapper.getChildrenCategoryByParentId(parentId);
       return list;
    }

    // 分类树
    @Override
    public List<CategoryTree> getTree() {
        List<Category> list = categoryMapper.getList();
        Map<Integer,List<CategoryTree>> map = new HashMap<>();
        for (Category category : list) {
            CategoryTree categoryTree = new CategoryTree();
            categoryTree.setValue(category.getId());
            categoryTree.setLabel(category.getName());
            Integer parentId = category.getParentId();
            if (!map.containsKey(parentId)){
                List<CategoryTree> categoryTrees = new ArrayList<>();
                categoryTrees.add(categoryTree);
                map.put(parentId,categoryTrees);
            }else {
                map.get(parentId).add(categoryTree);
            }
        }
        // 获得一级分类
        List<CategoryTree> categoryTrees = map.get(0);
        for (CategoryTree categoryTree : categoryTrees) {
            List<CategoryTree> categoryTrees1 = map.get(categoryTree.getValue());
            categoryTree.setChildren(categoryTrees1);
        }
        return categoryTrees;
    }

    @Override
    public List<CategoryListVO> showCategory() {
        return categoryMapper.showCategory();
    }


}
